package com.javaclimb.filmbuy.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.javaclimb.filmbuy.common.Consts;
import com.javaclimb.filmbuy.entity.AdminInfo;
import com.javaclimb.filmbuy.entity.AuthorityInfo;
import com.javaclimb.filmbuy.entity.UserInfo;
import com.javaclimb.filmbuy.service.AdminInfoService;
import com.javaclimb.filmbuy.common.ResultCode;
import com.javaclimb.filmbuy.entity.Account;
import com.javaclimb.filmbuy.common.Result;
import com.javaclimb.filmbuy.exception.CustomException;
import com.javaclimb.filmbuy.service.UserInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/*
* 账号相关的控制器
* */
@RestController
public class AccountController {

    //所有权限,json解析,当前登录用户解析后获取自己的权限
    @Value("${authority.info}")
    private String authorityStr;

    @Resource
    private AdminInfoService adminInfoService;
    @Resource
    private UserInfoService userInfoService;
    /*
    * 通用登录
    * */
    @PostMapping("/login")
    public Result<Account> login(@RequestBody Account account, HttpServletRequest request){
        if (account == null || StrUtil.isBlank(account.getName()) || StrUtil.isBlank(account.getPassword()) || account.getLevel() == null) {
            throw new CustomException(ResultCode.PARAM_LOST_ERROR);
        }

        Account login = new Account();

        if(account.getLevel() == 1){
            login = adminInfoService.login(account.getName(), account.getPassword());
        }

        if(account.getLevel() == 2){
            login = userInfoService.login(account.getName(), account.getPassword());
        }

        request.getSession().setAttribute(Consts.USER, login);

        return Result.success(login);
    }

    /*
     * 获取用户登录信息
     * */
    @GetMapping("/auth")
    public Result auth(HttpServletRequest request){
        Object user = request.getSession().getAttribute(Consts.USER);
        if(user == null){
            return Result.error("401", "未登录");
        }
        return Result.success(user);
    }

    /*
     * 用户退出
     * */
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        request.getSession().setAttribute(Consts.USER, null);
        return Result.success();
    }


    //获取当前登录用户所能看到的模块信息
    @GetMapping("/authority")
    public Result<List<Integer>> authority(HttpServletRequest request){
        Account user = (Account) request.getSession().getAttribute(Consts.USER);
        if(user==null){
            return Result.success(new ArrayList<>());
        }
        JSONArray objects = JSONUtil.parseArray(authorityStr);
        for (Object object:objects) {
            JSONObject jsonObject = (JSONObject)object;
            if(user.getLevel().equals((jsonObject.getInt("level")))){
                JSONArray array = JSONUtil.parseArray(jsonObject.getStr(  "models"));
                List<Integer> modelIdList = array.stream().map(o -> {
                    JSONObject obj = (JSONObject) o;
                    return obj.getInt(  "modelId");
                }).collect(Collectors.toList());
                return Result.success(modelIdList);
            }
        }
        return Result.success(new ArrayList<>());
    }

    /**
     *获取当前登录用户具有的当前模块的权限列表
     */
    @GetMapping("/permission/{modelId}")
    public Result<List<Integer>> permission(@PathVariable Integer modelId, HttpServletRequest request){
        Account user = (Account) request.getSession().getAttribute(Consts.USER);
        if(user==null){
            return Result.success(new ArrayList<>());
        }
        List<AuthorityInfo> authorityInfoList = JSONUtil.toList(JSONUtil.parseArray(authorityStr), AuthorityInfo.class);

        // 获取到当前level相应的用户权限列表
        Optional<AuthorityInfo> optional = authorityInfoList.stream().filter(x -> x.getLevel().equals(user.getLevel())).findFirst();
        // 非空判断
        if (optional.isPresent()){
            Optional<AuthorityInfo.Model> firstOption = optional.get().getModels().stream().filter(x -> x.getModelId().equals(modelId)).findFirst();

            if(firstOption.isPresent()){
                List<Integer> operation = firstOption.get().getOperation();
                return Result.success(operation);
            }
        }
        return Result.success(new ArrayList<>());
    }

    //获取角色列表
    @GetMapping("/getAuthority")
    public Result<List<AuthorityInfo>> getAuthority(){
        List<AuthorityInfo> authorityInfoList = JSONUtil.toList(JSONUtil.parseArray(authorityStr), AuthorityInfo.class);
        return Result.success(authorityInfoList);
    }
    //重置密码为123456
    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody Account account, HttpServletRequest request){
        Integer level = account.getLevel();
        if(level == 1){
            AdminInfo info = adminInfoService.findByUserName(account.getName());
            if(info == null){
                return Result.error(ResultCode.USER_NOT_EXIST_ERROR.code, ResultCode.USER_NOT_EXIST_ERROR.msg);
            }
            info.setPassword(SecureUtil.md5("123456"));
            adminInfoService.update(info);
        }
        if(level == 2){
            UserInfo info = userInfoService.findByUserName(account.getName());
            if(info == null){
                return Result.error(ResultCode.USER_NOT_EXIST_ERROR.code, ResultCode.USER_NOT_EXIST_ERROR.msg);
            }
            info.setPassword(SecureUtil.md5("123456"));
            userInfoService.update(info);
        }
        return Result.success();
    }


    /**
     * 获取当前用户登录信息
     */
    @GetMapping("/getAccountInfo")
    public Result<Object> getAccountInfo(HttpServletRequest request){
        Account account = (Account) request.getSession().getAttribute(Consts.USER);
        if(account == null){
            return Result.success(new Object());
        }
        Integer level = account.getLevel();
        if(level == 1){
            return Result.success(adminInfoService.findById(account.getId()));
        }
        if(level == 2){
            return Result.success(userInfoService.findById(account.getId()));
        }
        return Result.success(new Object());
    }


    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account info,HttpServletRequest request){
        Account account = (Account) request.getSession().getAttribute(Consts.USER);
        if(account == null){
            return Result.error(ResultCode.USER_NOT_EXIST_ERROR.code,ResultCode.USER_NOT_EXIST_ERROR.msg);
        }
        String oldPassword = SecureUtil.md5(info.getPassword());
        if (!oldPassword.equals(account.getPassword())){
            return Result.error(ResultCode.PARAM_PASSWORD_ERROR.code,ResultCode.PARAM_PASSWORD_ERROR.msg);
        }
        info.setPassword(SecureUtil.md5(info.getNewPassword()));
        Integer level = account.getlevel();
        if (level == 1){
            AdminInfo adminInfo = new AdminInfo();
            BeanUtils.copyProperties(info,adminInfo);
            adminInfoService.update(adminInfo);
        }
        if (level == 2){
            UserInfo userInfo = new UserInfo();
            BeanUtils.copyProperties(info,userInfo);
            userInfoService.update(userInfo);
        }
        //清空session，让用户重新登陆
        request.getSession().setAttribute(Consts.USER,null);
        return Result.success();
    }
}













